//
//  XMPhoneCodeViewController.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/8.
//

#import "XMPhoneCodeViewController.h"
#import "XMPhoneCodeView.h"
#import "XMNetwork+login.h"

@interface XMPhoneCodeViewController ()

@property (nonatomic, strong) XMPhoneCodeView *phoneCodeView;

@end

@implementation XMPhoneCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

- (void)creatUI {
    [self.view addSubview:self.phoneCodeView];
    //重新获取验证码
    XMWeakSelf;
    _phoneCodeView.reGetCodeBlock = ^{
        [weakSelf getCode];
    };
    //登录
    _phoneCodeView.loginBlock = ^(NSString * _Nonnull codeStr) {
//        [weakSelf validateCodeWithCode:codeStr];
        if ([weakSelf.codeType isEqualToString:@"0"]) {
            [weakSelf loginWithCode:codeStr];
        }
        else {
            [weakSelf bindPhoneWithCodeStr:codeStr];
        }
    };
}

//获取验证码
- (void)getCode {
//    XMWeakSelf;
    [_Network getCodeWithPhoneNum:_phoneNumStr
                             type:@"1"
                          success:^(id _Nonnull responseObject) {
        [MBProgressHUD showSuccess:@"发送成功"];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//校验验证码
- (void)validateCodeWithCode:(NSString *)codeStr {
    XMWeakSelf;
    [_Network valieCodeWithPhoneNum:_phoneNumStr
                            codeStr:codeStr
                            success:^(id  _Nonnull responseObject) {
        if ([weakSelf.codeType isEqualToString:@"0"]) {
            [weakSelf loginWithCode:codeStr];
        }
        else {
            [weakSelf bindPhoneWithCodeStr:codeStr];
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//登录
- (void)loginWithCode:(NSString *)codeStr {
    XMWeakSelf;
    [_Network loginWithPhone:_phoneNumStr
                        code:codeStr
                     success:^(id  _Nonnull responseObject) {
        XMUserModel *userModel = [XMUserModel yy_modelWithJSON:[responseObject objectForKey:@"data"]];
        [[XMUserManager shareInstance] saveUserModel:userModel];
        [weakSelf.navigationController dismissViewControllerAnimated:YES completion:nil];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)bindPhoneWithCodeStr:(NSString *)codeStr {
    [self wechatLogin];
//    XMWeakSelf;
//    [_Network bindPhoneWithCode:codeStr
//                        success:^(id  _Nonnull responseObject) {
////        [weakSelf.navigationController dismissViewControllerAnimated:YES completion:nil];
//        [weakSelf wechatLogin];
//    } failure:^(NSError * _Nonnull error) {
//
//    }];
}

//微信登录
- (void)wechatLogin {
    XMWeakSelf;
    [_Network wechatLoginWithAccessToken:self.accessToken
                                  openId:self.openId
                                 unionId:self.unionidStr
                                   phone:self.phoneNumStr
                                 success:^(id  _Nonnull responseObject) {
        XMUserModel *userModel = [XMUserModel yy_modelWithJSON:[responseObject objectForKey:@"data"]];
        [[XMUserManager shareInstance] saveUserModel:userModel];
        [weakSelf.navigationController dismissViewControllerAnimated:YES completion:nil];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (XMPhoneCodeView *)phoneCodeView {
    if (!_phoneCodeView) {
        _phoneCodeView = [[XMPhoneCodeView alloc] initWithFrame:self.view.bounds];
        [_phoneCodeView setContentWithPhoneNum:_phoneNumStr];
    }
    return _phoneCodeView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
